<script setup>
import { ref, onMounted } from 'vue'
import DataTable from '../components/common/DataTable.vue'

// 列配置
const columns = [
  { prop: 'id', label: 'ID', width: '80', sortable: true },
  { prop: 'house', label: '房屋', sortable: true },
  { prop: 'owner', label: '业主' },
  { prop: 'fee', label: '费用(元)', sortable: true },
  { prop: 'period', label: '缴费周期' },
  { prop: 'dueDate', label: '截止日期', type: 'date', sortable: true },
  { 
    prop: 'status', 
    label: '状态', 
    type: 'status',
    statusMap: {
      'paid': { label: '已缴费', type: 'success' },
      'unpaid': { label: '未缴费', type: 'danger' },
      'overdue': { label: '已逾期', type: 'warning' }
    }
  },
]

// 模拟数据
const tableData = ref([])
const loading = ref(false)

// 生成模拟数据
const generateMockData = () => {
  const data = []
  const statusOptions = ['paid', 'unpaid', 'overdue']
  const buildings = ['A栋', 'B栋', 'C栋']
  const periods = ['2024年第一季度', '2024年第二季度', '2024年第三季度', '2024年第四季度']
  
  for (let i = 1; i <= 55; i++) {
    const building = buildings[Math.floor(Math.random() * buildings.length)]
    const unit = Math.floor(Math.random() * 10) + 1
    const room = Math.floor(Math.random() * 5) + 1
    const houseNumber = `${building}${unit}0${room}`
    
    data.push({
      id: i,
      house: houseNumber,
      owner: `业主${i}`,
      fee: Math.floor(Math.random() * 5000) + 300,
      period: periods[Math.floor(Math.random() * periods.length)],
      dueDate: new Date(2024, Math.floor(Math.random() * 12), Math.floor(Math.random() * 28) + 1).toISOString(),
      status: statusOptions[Math.floor(Math.random() * statusOptions.length)],
      comments: `备注信息${i}`
    })
  }
  
  return data
}

// 事件处理器
const handleRowAdd = (row) => {
  // 在真实环境中，这里应该是API调用
  console.log('添加行:', row)
  // 生成ID并添加到表格
  const newRow = {
    ...row,
    id: tableData.value.length + 1,
    dueDate: row.dueDate || new Date().toISOString(),
    status: row.status || 'unpaid'
  }
  tableData.value.push(newRow)
}

const handleRowEdit = (row) => {
  // 在真实环境中，这里应该是API调用
  console.log('编辑行:', row)
  const index = tableData.value.findIndex(item => item.id === row.id)
  if (index !== -1) {
    tableData.value[index] = row
  }
}

const handleRowDelete = (row) => {
  // 在真实环境中，这里应该是API调用
  console.log('删除行:', row)
  tableData.value = tableData.value.filter(item => item.id !== row.id)
}

// 模拟加载数据
const loadData = () => {
  loading.value = true
  // 模拟网络请求延迟
  setTimeout(() => {
    tableData.value = generateMockData()
    loading.value = false
  }, 500)
}

onMounted(() => {
  loadData()
})
</script>

<template>
  <div class="property-fee-container">
    <div class="page-header bg-white p-4 rounded-lg shadow-sm mb-4">
      <h1 class="text-xl font-bold text-gray-800">物业费管理</h1>
      <p class="text-gray-500 mt-1">查看和管理所有住户的物业费缴纳情况</p>
    </div>
    <div class="data-table-wrapper bg-white p-4 rounded-lg shadow-sm">
      <DataTable
        :data="tableData"
        :columns="columns"
        :loading="loading"
        @row-add="handleRowAdd"
        @row-edit="handleRowEdit"
        @row-delete="handleRowDelete"
      />
    </div>
  </div>
</template>

<style scoped>
.property-fee-container {
  min-height: 100%;
}

.data-table-wrapper {
  overflow: auto;
}
</style> 